import json
import time
import logging
import datetime
import pandas as pd

import QUANTAXIS as QA
from QUANTAXIS.QAARP import QA_Risk
from QIFIAccount.QIFIAccount.QARealtimeStockSim import QIFI_StockSIM_Account
from QAStrategy.QAStrategy.classic_strategy.turtle_strategy import TurtleTrade


def main():
    # account
    username = 'admin'
    password = 'admin'

    # strategy
    print('start execute strategy')
    today = datetime.date.today()
    last_week = datetime.date.today() + datetime.timedelta(days=-30)
    last_year = datetime.date.today() + datetime.timedelta(days=-365)

    start = last_week.strftime('%Y-%m-%d')
    end = today.strftime('%Y-%m-%d')
    end = QA.QA_util_get_pre_trade_date(end, n=1)
    start = '2019-04-20'
    end = '2021-01-18'
    code = '601933'
    market = QA.MARKET_TYPE.STOCK_CN
    strategy = TurtleTrade(username='turtle_backtest_stock_day', password='123456', code=code,
                         market=market, start=start, end=end, show_trade=True,
                         min_trade_money=3000,
                         frequence=QA.FREQUENCE.DAY, strategy_id=username, init_cash=1e5,
                         ratio=0.8, n_up=20, n_down=20, n_short_ma=10, n_long_ma=60, n_atr=20)
    strategy.run_backtest()
    strategy.plot_indicators()
    risk = QA_Risk(strategy.acc, benchmark_code=code, benchmark_type=market)
    risk.plot_assets_curve()

if __name__ == "__main__":
    try:
        from QUANTAXIS_RealtimeCollector.QARealtimeCollector.utils.logconf import update_log_file_config
        logfile = 'stock.backtest.log'
        logging.config.dictConfig(update_log_file_config(logfile))
    except Exception as e:
        print(e.__str__())
    # main_muti()
    main()